package com.ld.admin.controller.oa;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.oa.OaBooksBorrowRecordModel;
import com.ld.admin.model.oa.OaBooksManageModel;
import com.ld.admin.service.oa.OaBooksManageService;
import com.ld.shieldsb.annotation.duplicate.SubmitRequestException;
import com.ld.shieldsb.annotation.duplicate.SubmitRequestIntercept;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 书籍借阅管理
 * 
 * @ClassName OaBooksManageController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年06月12日 09:39:36
 *
 */
@Api(tags = "书籍管理")
@Controller(value = "oaOaBooksManageController")
@RequestMapping("${shieldsb.adminPath}/oa/booksmanage")
public class OaBooksManageController extends AdminBasicController<OaBooksManageModel> {
    @Autowired
    private OaBooksManageService bookService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "a/oa/booksmanage/oabooksmanage_header";
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/oa/booksmanage/oabooksmanage_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = basicService.getSearchQueryModel(request);
        basicService.getPageBean(request, getModelClass(), queryModel);
        return "a/oa/booksmanage/oabooksmanage_list";
    }

    @ApiOperation(value = "书籍展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        basicService.getOne(request, getModelClass(), id);
        return "a/oa/booksmanage/oabooksmanage_show";
    }

    @ApiOperation(value = "跳转到添加/修改书籍", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        basicService.getOne(request, getModelClass(), id);
        return "a/oa/booksmanage/oabooksmanage_update";
    }

    @SubmitRequestIntercept(type = SubmitRequestIntercept.SESSION)
    @ApiOperation(value = "添加/修改书籍", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object addOrUpd(@Valid OaBooksManageModel model) throws SubmitRequestException {
        Result result = basicService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除书籍", notes = "")
    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = basicService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    // ===============================借阅历史===============================
    @ApiOperation(value = "跳转到书籍借阅记录", notes = "")
    @RequestMapping(path = "/borrowrecord/show", method = RequestMethod.GET)
    public String toShowRecord() {
        setAttributes();
//        long bookid = getLong("bid");
//        QueryModel queryModel = basicService.getSearchQueryModel(request);
//        queryModel.addEq("bookid", bookid);
//        queryModel.setOrder("borrowTime", true);
//        basicService.getPageBean(request, OaBooksBorrowRecordModel.class, queryModel, getInt("p"), 5);
        return "a/oa/booksmanage/borrowrecord/borrowrecord_show";
    }

    @ApiOperation(value = "搜索书籍借阅记录", notes = "")
    @RequestMapping(path = "/borrowrecord/search", method = RequestMethod.POST)
    public String borrowrecordSearch() {
        setAttributes();
        long bookid = getLong("bid");
        QueryModel queryModel = basicService.getSearchQueryModel(request, getModelClass());
        queryModel.addEq("bookid", bookid);
        queryModel.setOrder("borrowTime", true);
        basicService.getPageBean(request, OaBooksBorrowRecordModel.class, queryModel);
        return "a/oa/booksmanage/borrowrecord/borrowrecord_list";
    }

    @ApiOperation(value = "添加/修改书籍借阅记录", notes = "")
    @RequestMapping(path = "/borrowrecord/addOrUpd", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object borrowrecordAddOrUpd(@Valid OaBooksBorrowRecordModel model) {
        Result result = bookService.addOrUpdateBorrowRecord(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "删除书籍借阅记录", notes = "")
    @RequestMapping(path = "/borrowrecord/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object borrowrecordDel() {
        long id = getLong("id");
        Result result = basicService.remove(OaBooksBorrowRecordModel.class, id);
        return Web.Response.result(result);
    }

    // ===============================我的借阅信息===============================
    @ApiOperation(value = "到我的借阅信息搜索页面", notes = "")
    @RequestMapping(path = "/myborrow/toSearch", method = RequestMethod.GET)
    public String myborrow2Search() {
        return "a/oa/booksmanage/myborrow/myborrow_search";
    }

    @ApiOperation(value = "我的借阅信息搜索结果", notes = "")
    @RequestMapping(path = "/myborrow/search", method = RequestMethod.POST)
    public String myborrowSearch() {
        bookService.getMyBorrowPageBean();
        return "a/oa/booksmanage/myborrow/myborrow_list";
    }

    @ApiOperation(value = "跳转到添加/修改我的借阅记录", notes = "")
    @RequestMapping(path = "/myborrow/toAddOrUpdate", method = RequestMethod.GET)
    public String myborrow2AddOrUpd() {
        long id = getLong("id");
        basicService.getOneAndLinks(request, OaBooksBorrowRecordModel.class, id);
        return "a/oa/booksmanage/myborrow/myborrow_update";
    }

    @ApiOperation(value = "添加/修改我的借阅记录", notes = "")
    @RequestMapping(path = "/myborrow/addOrUpd", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object myborrowAddOrUpd(@Valid OaBooksBorrowRecordModel model) {
        Result result = bookService.addOrUpdateBorrowRecord(model, true); // 只更新
        return Web.Response.result(result);
    }

}